package uk.ac.ebi.bioinvindex.model.impl;

import uk.ac.ebi.bioinvindex.model.impl.term.PropertyImpl;
import uk.ac.ebi.bioinvindex.model.impl.term.ProtocolTypeImpl;
import uk.ac.ebi.bioinvindex.model.impl.term.Parameter;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * User: Nataliya Sklyar (nsklyar@ebi.ac.uk)
 * Date: Nov 20, 2007
 */
@Entity
@Table(name = "protocol")
public class ProtocolImpl extends AnnotatableImpl{

	private String name;
	private ProtocolTypeImpl type;
	private String description;
	private List<Parameter> parameters = new ArrayList<Parameter>();

	protected ProtocolImpl() {
		super();
	}

	public ProtocolImpl(String name, ProtocolTypeImpl type) {
		this.name = name;
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToOne(targetEntity = ProtocolTypeImpl.class)
	@JoinColumn(name = "type")
	public ProtocolTypeImpl getType() {
		return type;
	}

	public void setType(ProtocolTypeImpl type) {
		this.type = type;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@OneToMany(
			targetEntity = PropertyImpl.class
	)
	@JoinTable(
			name = "Protocol2Property",
			joinColumns = {@JoinColumn(name = "PROTOCOL_ID")},
			inverseJoinColumns = @JoinColumn(name = "PROPERTY_ID")
	)
	public List<Parameter> getParameters() {
		return parameters;
	}

	public void addParameter(Parameter parameter) {
		parameters.add(parameter);
	}

	protected void setParameters(List<Parameter> parameters) {
		this.parameters = parameters;
	}

	public boolean removeParameter(Parameter parameter) {
		return getParameters().remove(parameter);
	}

	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		if (!super.equals(o)) return false;

		ProtocolImpl protocol = (ProtocolImpl) o;

		if (description != null ? !description.equals(protocol.description) : protocol.description != null) return false;
		if (!name.equals(protocol.name)) return false;
		if (parameters != null ? !parameters.equals(protocol.parameters) : protocol.parameters != null) return false;
		if (!type.equals(protocol.type)) return false;

		return true;
	}

	public int hashCode() {
		int result = super.hashCode();
		result = 31 * result + name.hashCode();
		result = 31 * result + type.hashCode();
		result = 31 * result + (description != null ? description.hashCode() : 0);
		result = 31 * result + (parameters != null ? parameters.hashCode() : 0);
		return result;
	}

		public String toString() {
		return "ProtocolImpl{" +
				"acc='" + getAcc() + '\'' +
				", name=" + type +
				", type=" + type +
				", description='" + description + '\'' +
				", parameters=" + parameters +
				'}';
	}
}
